package org.darkforest.merchant.controller;

import org.apache.commons.lang3.StringUtils;
import org.darkforest.dao.mapper.SpecMapper;
import org.darkforest.dao.mapper.SpecValueMapper;
import org.darkforest.model.Spec;
import org.darkforest.model.SpecValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by Administrator on 2017/4/1.
 */
@Controller
@RequestMapping(value = "specValue")
public class SpecValueController {

    @Autowired
    private SpecValueMapper specValueMapper;

    @Autowired
    private SpecMapper specMapper;


    @RequestMapping(value = "findSpecValueBySpId")
    public ModelAndView findSpecValueBySpId(@RequestParam(required = true)Integer spId,
                                            @RequestParam(required = false,defaultValue = "1") int p,
                                            @RequestParam(required = true)Integer spParentId
    ){
        ModelAndView res = new ModelAndView("spec/spec_value_list");

        SpecValue specValue = new SpecValue();
        specValue.setSpId(spId);
        specValue.setStart(p);
        specValue.setPageSize(10);
        Spec spec = specMapper.selectByPrimaryKey(spId);
        List<SpecValue> specValueList = specValueMapper.findSpecValue(specValue);
        specValue.setResultList(specValueList);
        res.addObject("specValueInfo", specValue);
        res.addObject("spec",spec);
        res.addObject("spParentId",spParentId);
        return res;
    }



    @RequestMapping(value = "delSpecValue")
    public @ResponseBody boolean delSpecValue(Integer spValueIds){
        specValueMapper.deleteByPrimaryKey(spValueIds);
        return true;
    }


    @RequestMapping(value = "doAddSpValues")
    public @ResponseBody boolean doAddSpValues(Spec spec){
        if(spec!=null){
            for(SpecValue specValue:spec.getSpecValueList()){
                if(StringUtils.isBlank(specValue.getSpValueName()))
                    continue;
                specValueMapper.insertSelective(specValue);
            }
        }
        return true;
    }
}
